package com.ht.utils.htmlBean;

import com.ht.utils.config.WordAndHtmlConfig;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
@Data
public class PPR {
    private String fontFamily;
    private String fontSize;
    private String fontWeight;
    private String fontStyle;
    private String fontColor;
    private String numName;
    private String numberFormat;
    private String numberFormatText;
    private String highlight;
    private String shd;
    private Ind ind;
    private String align = "both";
    private String titleLev;
    private List<RPR> rprs = new ArrayList<>();
    private boolean isBold = false;
    private boolean isXiaHua = false;
    private boolean isXieTi = false;
    private boolean isShanChu = false;
    public void setFontSize(String fontSize) {
        double temp = Double.parseDouble(fontSize);
        this.fontSize = temp + "";
    }
    // 设置标题样式
    public void setTitleLev(String titleVal) {
        switch (titleVal){
            case "1":
                this.titleLev = "0";
                this.fontFamily = "宋体";
                this.fontSize = "36.3";
                this.isBold = true;
                break;
            case "2":
                this.titleLev = "1";
                this.fontFamily = "宋体";
                this.fontSize = "29.3";
                break;
            case "3":
                this.titleLev = "2";
                this.fontFamily = "黑体";
                this.fontSize = "21.3";
                break;
            case "4":
                this.titleLev = "3";
                this.fontFamily = "宋体";
                this.fontSize = "21.3";
                break;
            case "5":
                this.titleLev = "4";
                this.fontFamily = "黑体";
                this.fontSize = "18.6";
                break;
            case "6":
                this.titleLev = "5";
                this.fontFamily = "宋体";
                this.fontSize = "18.6";
                break;
            case "7":
                this.titleLev = "6";
                this.fontFamily = "黑体";
                this.fontSize = "16";
                break;
            case "8":
                this.titleLev = "7";
                this.fontFamily = "宋体";
                this.fontSize = "16";
                break;
            default:
                this.titleLev = titleVal;
        }
    }
    public void addRprs(RPR rpr){
        this.rprs.add(rpr);
    }

    String toHtml(){
        boolean isProjectNum = false;
        String p = "p";
        if(this.numberFormatText != null
                && (this.numberFormatText.equals("disc") || this.numberFormatText.equals("square"))){
            isProjectNum = true;
            p = "li";
        }

        // 处理标题，方便页面生成目录
        if (this.titleLev != null){
            switch(this.titleLev){
                case "0":
                    p = "h1";
                    break;
                case "1":
                    p = "h2";
                    break;
                case "3":
                    p = "h3";
                    break;
                default:
                    break;
            }
        }

        StringBuilder html = new StringBuilder("<" + p + " style=\"");

        if(fontFamily != null){
            html.append(" font-family:'").append(WordAndHtmlConfig.getZiTiByWord(fontFamily)).append("'; ");
        }
        if(fontColor!=null){
            html.append(" color:#").append(fontColor).append("; ");
        }
        if(fontSize != null){
            html.append(" font-size: ").append(fontSize).append("px; ");
        }
        if(this.shd != null){
            html.append(" background-color: #").append(this.shd)
                    .append(";     margin: 0!important; line-height: 32px;");
        }
        if(this.highlight != null){
            html.append(" background-color: #").append(this.highlight).append("; ");
        }
        if(this.ind != null){
            html.append(this.ind.toHtml());
        }
        if(isBold){
            html.append(" font-weight:bold; ");
        }
        if(isShanChu){
            html.append(" text-decoration:line-through; ");
        }
        if(isXiaHua){
            html.append(" text-decoration:underline; ");
        }
        if(isXieTi){
            html.append(" font-style:italic; ");
        }
        if(!"both".equals(align)){
            html.append(" text-align:").append(align).append("; ");
        }
        html.append(" \" ");
        if(titleLev!=null){
            html.append(" data-class='").append(titleLev).append("' ");
        }
        if(this.numberFormat!=null){
            html.append(" num-class='").append(this.numberFormat).append("'");
        }
        if(isProjectNum){
            html.append(" type='").append(this.numberFormatText).append("' ");
        }
        html.append(">");
        if(!isProjectNum){
            if(this.numberFormatText != null){
                html.append("<i>").append(this.numberFormatText).append(" </i>");
            }
        }
        if(rprs != null && !rprs.isEmpty()){
            for(RPR rpr:rprs){
                html.append(rpr.toHtml());
            }
        }
        html.append("</").append(p).append(">");
        return html.toString();
    }

}
